#数据库访问对象  DB Access Object
import pymysql

class dao:
    def __init__(self, DBName):
        self.db = pymysql.connect(host='localhost',user='root',password='',db=DBName,charset = 'utf8')
        self.cursor = self.db.cursor()

    def queryall(self):
        sql = 'select * from stu;'
        try:
            self.cursor.execute(sql)
            return self.cursor.fetchall()
        except Exception as err1:
            print('sql exception:',err1)

    def insertStu(self,stu):
        sql = "insert into stu(name,age,classid) values('%s','%d','%s')"%(stu)
        try:
            result = self.cursor.execute(sql)
            self.db.commit()
            print('成功插入数据数：',result)
        except Exception as err2:
            print('sql exception:',err2)

    def delStu(self,id):
        sql = "delete from stu where id = '%d';"%(id)
        try:
            result = self.cursor.execute(sql)
            self.db.commit()
            print('成功删除数据数：',result)
        except Exception as err3:
            print('sql exception:',err3)


